package com.craney.util
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	
	public class ControlProgressBar extends ProgressBar
	{
		/**
		 * 控制按钮的图形 
		 */		
		protected var controlBar:Sprite = new Sprite();
		
		/**
		 * 可控制Sound和SoundChannel的类 
		 */		
		protected var soundPlay:ISoundPlay;
		
		
		public function ControlProgressBar(barWidth:uint=100, barHeight:uint=15, currentColor:uint=0x00FF00, totalColor:uint=0x0000FF, currentColorAlpha:Number=1.0, totalColorAlpha:Number=1.0)
		{
			super(barWidth, barHeight, currentColor, totalColor, currentColorAlpha, totalColorAlpha);
			
//			this.mouseChildren = false;
			
			controlBar.buttonMode = true;
			controlBar.useHandCursor = true;
			addChild(controlBar);
		}
		
		public function drawController(percentage:Number):void
		{
			controlBar.graphics.clear();
			controlBar.graphics.beginFill(0x000000);
			controlBar.graphics.drawRect(barWidth*percentage-1, 0, 4, 10); 
		}
		
		
		/**
		 * 
		 * 当点击进度条时,播放跟着改变
		 * @param event
		 * 
		 */		
		private function changePlay(event:MouseEvent):void
		{
//			if(event.currentTarget == this)
			{
				trace("click" + event.currentTarget);
				var percentage:Number = event.localX / barWidth;
				//移动进度条
				drawController(percentage);
				
				//改变当前播放进程
				soundPlay.playFrom(percentage);
			}
		}
		/**
		 * 
		 * 点击进度条,改变播放
		 * @param iSoundAndChannel 可以得到Sound和SoundChannel实例的接口实现类
		 * 
		 */		
		public function addClickControl(soundPlay:ISoundPlay):void
		{
			//选择播放监听
			addEventListener(MouseEvent.CLICK, changePlay);
			
			addEventListener(MouseEvent.MOUSE_DOWN, mouseHandler);
			addEventListener(MouseEvent.MOUSE_UP, mouseHandler);
			controlBar.addEventListener(MouseEvent.MOUSE_DOWN, mouseHandler);
			controlBar.addEventListener(MouseEvent.MOUSE_UP, mouseHandler);
			this.soundPlay = soundPlay;

		}
		
		private function mouseHandler(e:MouseEvent):void
		{
			trace(e.type + e.currentTarget);
		}
	}
}